package com.jd.fruit.controller.admin.shop;

import java.io.IOException;

import com.alibaba.fastjson.JSONArray;
import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.DictUtil;
import com.jd.fruit.common.util.StringUtil;
import com.jd.fruit.model.Chnl;
import com.jd.fruit.model.admin.order.Order;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 订单管理
 * 
 * @author admin
 * 
 */
@Controller("/admin/shop/order")
// 这个路径就是页面的路径
public class OrderController extends BaseController {
	/**
	 * 
	 */
	public void index() {

		render("order_index.html");
	}

	/**
	 * 订单编辑
	 */
	public void edit() {
		String id = StringUtil.getDcodePara(getRequest(), "order_id");
		Order order = Order.dao.findById(id);
		this.getRequest().setAttribute("order", order);
		render("order_edit.html");// 这个是页面的命名规范
	}

	/**
	 * 订单删除
	 * 
	 * @throws IOException
	 */
	public void delete() throws IOException {
		String id = StringUtil.getDcodePara(getRequest(), "order_id");

			boolean isOk = Order.dao.deleteById(id);
			if (isOk) {// success
				this.renderText(DictUtil.success);
			} else { // failed
				this.renderText(DictUtil.failed);
			}
	}

	/**
	 *订单保存信息
	 */
	public void save() {
		Order order = getModel(Order.class);
		Order.dao.save(order);
		this.redirect("/order/list");
	}
	/**
	 * 订单修改
	 */
	public void update(Order order){
		Record	user = Db.findById("order_id", getPara("order_id")).set("order_no", getPara("")).set("", getPara(""));
		Db.update("user", user);
		Order.dao.update();
	}
	/**
	 *订单列表
	 */
	public void list() {
		int pageNumber = this.getParaToInt(0,1);
		Page<Order> orders = Order.dao.list(pageNumber,DictUtil.default_pageSize);
		this.setAttr("orders", orders);
		render("order_list.html");
	}
	
	/**
	 * 加载栏目Jason数据
	 * 
	 * @throws IOException
	 */
	public void loadOrderData() {
		String type = getPara("type");
		String result = "";
		JSONArray jsarray = null;
		if (!StrKit.isBlank(type) && type.equals(DictUtil.jsontree)) {
			jsarray = Chnl.dao.loadTree();
			if (jsarray != null) {
				result = jsarray.toJSONString();
			}
		} else {
			result = Chnl.dao.loadTreeGrid();
		}
		renderJson(result);
	}
	


}
